#include"StdAfx.h"

//ID3D11InputLayout *pgVertexShader::m_pVertexInputLayout = NULL;

pgVertexShader::pgVertexShader()
{
	m_pVertexShader = NULL;
	m_pVertexInputLayout = NULL;
}

pgVertexShader::~pgVertexShader()
{
}

bool pgVertexShader::CompileFromFile(LPCWSTR szFilePath)
{
	PG_LOG_INFO_EX(TOPIC_PGRENDERER, "Compiling vertex shader from file: %s!", pgString::LPCWSTRToStdString(szFilePath).c_str());

	DWORD dwShaderFlags = D3DCOMPILE_ENABLE_STRICTNESS;
#if defined( DEBUG ) || defined( _DEBUG )
    // Set the D3DCOMPILE_DEBUG flag to embed debug information in the shaders.
    // Setting this flag improves the shader debugging experience, but still allows 
    // the shaders to be optimized and to run exactly the way they will run in 
    // the release configuration of this program.
    dwShaderFlags |= D3DCOMPILE_DEBUG;
#endif

	ID3DBlob* pErrorBlob = NULL;
	ID3DBlob *pVSBlob = NULL;


	HRESULT hr = D3DX11CompileFromFile(szFilePath, NULL, NULL, "VS", "vs_4_0", 
        dwShaderFlags, 0, NULL, &pVSBlob, &pErrorBlob, NULL );
    if( FAILED(hr) )
    {
        if( pErrorBlob != NULL )
            OutputDebugStringA( (char*)pErrorBlob->GetBufferPointer() );
        if( pErrorBlob ) pErrorBlob->Release();

		PG_DX_LOG_ERROR(hr, __FILE__, __LINE__);
		return false;
    }

    if( pErrorBlob ) pErrorBlob->Release();

	hr = g_PG_pRenderer->m_pd3dDevice->CreateVertexShader(pVSBlob->GetBufferPointer(), pVSBlob->GetBufferSize(), NULL, &m_pVertexShader);

	if (FAILED(hr))
	{
		pVSBlob->Release();
		PG_DX_LOG_ERROR(hr, __FILE__, __LINE__);
		return false;
	}

	hr = g_PG_pRenderer->m_pd3dDevice->CreateInputLayout(g_vertexLayout, g_numVertexLayoutElements,
		pVSBlob->GetBufferPointer(), pVSBlob->GetBufferSize(), &m_pVertexInputLayout);

	pVSBlob->Release();

	if (FAILED(hr))
	{
		PG_DX_LOG_ERROR(hr, __FILE__, __LINE__);
		return false;
	}


	return true;
}

void pgVertexShader::ReleaseShader()
{
	PG_SAFE_RELEASE(m_pVertexInputLayout);
	PG_SAFE_RELEASE(m_pVertexShader);
}